import SceneManager from "../../manager/SceneManager";
import LoadManager from "../../manager/LoadManager";
import HeroData from "../../data/HeroData";
import MenaryModel from "../../model/MenaryModel";
import HeroBox from "../../common/HeroBox";

const {ccclass, property} = cc._decorator;

@ccclass
export default class MenaryScene extends cc.Component {
    @property(cc.ScrollView)
    scrollView:cc.ScrollView = null;
    @property(cc.Sprite)
    img_select:cc.Sprite = null;
    @property(cc.Node)
    bottomHeroBox:cc.Node = null;

    @property(cc.Label)
    txt_lv:cc.Label = null;
    @property(cc.Label)
    txt_nextlv:cc.Label = null;
    @property(cc.Label)
    txt_atk:cc.Label = null;
    @property(cc.Label)
    txt_nextatk:cc.Label = null;
    @property(cc.Label)
    txt_def:cc.Label = null;
    @property(cc.Label)
    txt_nextdef:cc.Label = null;
    @property(cc.Label)
    txt_hp:cc.Label = null;
    @property(cc.Label)
    txt_nexthp:cc.Label = null;
    @property(cc.Label)
    txt_mp:cc.Label = null;
    @property(cc.Label)
    txt_nextmp:cc.Label = null;

    @property(cc.Label)
    txt_name:cc.Label = null;

    private selectHeroBox:cc.Node
    private heroBoxList:Array<cc.Node> = [];
    private onEnterMainScene() {
        SceneManager.instance.goto("MainScene")
    }
    private resetHeroBoxList() {
        let heroDataList:Array<HeroData> = MenaryModel.instance.heroDataList
        for (let index = 0; index < this.heroBoxList.length; index++) {
            const heroBox:cc.Node = this.heroBoxList[index];
            const heroData:HeroData = heroDataList[index]
            heroBox.getComponent(HeroBox).heroData = heroData;
        }
        let heroData:HeroData = this.selectHeroBox.getComponent(HeroBox).heroData
        this.setHeroInfo(heroData)
    }
    private onLevelUp() {
        if(!this.selectHeroBox)
        {
            return
        }
        let heroData:HeroData = this.selectHeroBox.getComponent(HeroBox).heroData
        MenaryModel.instance.onLevelUpHero(heroData.h_id,this.resetHeroBoxList.bind(this))
    }
    private setHeroInfo(heroData?:HeroData) {
        if (heroData)
        {
            this.txt_name.string = heroData.name
            this.txt_lv.string = String(heroData.lv)
            this.txt_nextlv.string = String(heroData.nextLvHeroData.lv)
            this.txt_atk.string = String(heroData.cur_atk)
            this.txt_nextatk.string = String(heroData.nextLvHeroData.cur_atk)
            this.txt_def.string = String(heroData.cur_def)
            this.txt_nextdef.string = String(heroData.nextLvHeroData.cur_def)
            this.txt_hp.string = String(heroData.cur_hp)
            this.txt_nexthp.string = String(heroData.nextLvHeroData.cur_hp)
            this.txt_mp.string = String(heroData.cur_mp)
            this.txt_nextmp.string = String(heroData.nextLvHeroData.cur_mp)
        }
        else
        {
            this.txt_name.string = ""
            this.txt_lv.string = ""
            this.txt_nextlv.string = ""
            this.txt_atk.string = ""
            this.txt_nextatk.string = ""
            this.txt_def.string = ""
            this.txt_nextdef.string = ""
            this.txt_hp.string = ""
            this.txt_nexthp.string = ""
            this.txt_mp.string = ""
            this.txt_nextmp.string = ""
        }
    }
     //选定
     public onSelect(event:cc.Event.EventTouch) {
        this.img_select.node.active=true;
        let node :cc.Node = event.getCurrentTarget();
        this.selectHeroBox = node;
        this.img_select.node.setPosition(this.selectHeroBox.x,this.selectHeroBox.y);
        this.scrollView.content.insertChild(this.img_select.node,this.heroBoxList.length+1);
        this.bottomHeroBox.active = true
        this.bottomHeroBox.getComponent(HeroBox).heroData = this.selectHeroBox.getComponent(HeroBox).heroData
        let heroData:HeroData = this.bottomHeroBox.getComponent(HeroBox).heroData
        this.setHeroInfo(heroData)
    }
    private initHeroBoxList() {
        let prefab :cc.Prefab = LoadManager.instance.getPrefabResByUrl("prefab/common/HeroBox");
        let heroDataList:Array<HeroData> = MenaryModel.instance.heroDataList
        for (let index = 0; index < heroDataList.length; index++) {
            const heroData:HeroData = heroDataList[index];
            let heroBox :cc.Node = cc.instantiate(prefab);
            this.heroBoxList.push(heroBox)
            this.scrollView.content.addChild(heroBox);
            heroBox.x = heroBox.width/2+(index%4)*(heroBox.width+20)+10;
            heroBox.y = -heroBox.height/2-Math.floor(index/4)*(heroBox.height+10);
            heroBox.getComponent(HeroBox).heroData = heroData;
            heroBox.on(cc.Node.EventType.TOUCH_END,this.onSelect,this)
        }
        this.img_select.node.active=false;
        this.selectHeroBox = null;
        this.bottomHeroBox.active = false
    }
    start () {
        this.initHeroBoxList()
        this.setHeroInfo()
    }

}
